﻿namespace WPForms.Phone.ViewModel
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Microsoft.Practices.Prism.Commands;
    using Microsoft.Practices.Prism.ViewModel;
    using PhoneUtils.Services;
    using PhoneUtils.ViewModel;
    using WPForms.Common.Model;
    using WPForms.Common.Services;

    /// <summary>
    /// View model for dispalying the list of available form instances.
    /// </summary>
    public class FormCollectionViewModel : NotificationObject, INavigationAware
    {
        /// <summary>
        /// References the form store.
        /// </summary>
        private IFormStore formStore;

        /// <summary>
        /// Reference to the navigation service.
        /// </summary>
        private INavigationService navigationService;

        /// <summary>
        /// Initializes a new instance of the <see cref="FormCollectionViewModel" /> class.
        /// </summary>
        /// <param name="formStore">The form store.</param>
        /// <param name="navigationService">The navigation service.</param>
        public FormCollectionViewModel(IFormStore formStore, INavigationService navigationService)
        {
            this.formStore = formStore;
            this.navigationService = navigationService;
        }

        /// <summary>
        /// Gets the list of form instances.
        /// </summary>
        public ReadOnlyCollection<FormInstanceDescription> Instances { get; private set; }

        /// <summary>
        /// Gets or sets the selected form.
        /// This property is used to select a form from the form collection list box.
        /// The getter always returns <c>null</c>.
        /// </summary>
        public FormInstanceDescription SelectedForm
        {
            get
            {
                return null;
            }

            set
            {
                if (value != null)
                {
                    this.navigationService.Navigate(new Uri("/WPForms.Phone;component/View/Form.xaml?formId=" + value.Id.ToString(), UriKind.Relative));
                }
            }
        }

        /// <summary>
        /// Raises the <see cref="E:NavigatedFrom"/> event.
        /// </summary>
        /// <param name="navigationArgs">The <see cref="System.Windows.Navigation.NavigationEventArgs"/> instance containing the event data.</param>
        /// <param name="context">The context.</param>
        public void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs navigationArgs, System.Windows.Navigation.NavigationContext context)
        {
        }

        /// <summary>
        /// Raises the <see cref="E:NavigatedTo"/> event.
        /// </summary>
        /// <param name="navigationArgs">The <see cref="System.Windows.Navigation.NavigationEventArgs"/> instance containing the event data.</param>
        /// <param name="context">The context.</param>
        public void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs navigationArgs, System.Windows.Navigation.NavigationContext context)
        {
            this.Instances = this.formStore.GetForms();
            this.RaisePropertyChanged(() => this.Instances);
        }

        /// <summary>
        /// Raises the <see cref="E:NavigatingFrom"/> event.
        /// </summary>
        /// <param name="navigationArgs">The <see cref="System.Windows.Navigation.NavigatingCancelEventArgs"/> instance containing the event data.</param>
        /// <param name="context">The context.</param>
        public void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs navigationArgs, System.Windows.Navigation.NavigationContext context)
        {
        }

        /// <summary>
        /// Raises the <see cref="E:FragmentNavigation"/> event.
        /// </summary>
        /// <param name="navigationArgs">The <see cref="System.Windows.Navigation.FragmentNavigationEventArgs"/> instance containing the event data.</param>
        /// <param name="context">The context.</param>
        public void OnFragmentNavigation(System.Windows.Navigation.FragmentNavigationEventArgs navigationArgs, System.Windows.Navigation.NavigationContext context)
        {
        }
    }
}
